﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MabuCommX;
using ReflectionStudio.Core.Events;
using System.Windows.Forms;
namespace WPFExcelReport
{
    public class IF2MACHINE
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public ReadWrong ReadWrong
        {
            get;
            set;
        }
        int wait = 300;
        public comm MABU
        {
            get;
            set;
        }
        public ReadCounterSpeed MABUJ
        {
            get;
            set;
        }
        public IF2MACHINE()
        {
            SetUp();
        }
        bool IsOnMachine = true;
        public void SetUp()
        {
            try
            {
                if (MABU == null)
                {
                    MABU = new comm();
                    MABUJ = new ReadCounterSpeed();
                    ReadWrong = new ReadWrong();
                    MABU.Settings = "19200,n,8,1";
                    MABU.wait = wait;
                    MABU.CommPort = 1;
                    MABU.CollectImpl = (_ICollect)MABUJ;
                    IsOnMachine = true;
                }
            }
            catch (Exception ex)
            {
                IsOnMachine = false;
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public double GetLength()
        {
            double qty = -1;           
           /* if (app.TERMNO.Trim()=="99")
            {
                Random rand = new Random();
                qty = 100 * rand.NextDouble();
                return qty;
            }
            */
            if (!IsOnMachine)
            {
                Random rand = new Random();
                qty = 10*rand.NextDouble(); 
                return qty;
            }
            try
            {
                MABU.wait = wait;
                //MABU.ExecuteCommand();
                if (!MABU.ExecuteCommand())
                    return -1;
               string LENGTH = Convert.ToString(MABUJ.Counter);
                if (MABUJ.UnitFlag.ToString() == "1")
                {
                    LENGTH =(Math.Round(Convert.ToDouble(LENGTH) * 0.9144,2)).ToString();
                }
                qty = System.Convert.ToDouble(LENGTH);              
            }
            catch (Exception ex)
            {
                //MESSAGE = ex.Message;
                //MessageBox.Show(ex.Message+LENGTH,ex.StackTrace);
                Tracer.Error(ex.StackTrace, ex);
            }
            return qty;
        }
    }
}

